﻿using System;
using System.Text;
using HslCommunication;
using HslCommunication.Core;

namespace NewUI.Service.Hsl;

public class PlcJuanCai : IDataTransfer
{
    // 字段定义
    public int Id;
    public string BianHao = "";
    public short HouDu;
    public short ChangDu;
    public short KuanDu;
    public short ZhiJing;
    public string Treatment = "";
    public string ZhongLiang = "0Kg";
    public string DengJi = "";
    public string JianYanYuan = "";
    public string RiQi = "1990-1-1";
    public string Code = "";
    public string PiCi = "";
    public string Print_Template = "测试";
    public string KeHu_Name = "";
    public short DJ_class;
    public bool No_Pack;
    public bool KH_OK;
    public bool Print_Top_OK;
    public bool Print_Body_OK;
    public bool Need_QPM;
    public bool Need_ZG;
    public bool Need_PMD;
    public bool IS_HouPMD;
    public bool Double_PMD;
    public bool Need_TieBiao_CM;
    public bool Need_TieBiao_Middle;
    public bool Multi_MiddleTieBiao;
    public bool LM_OK;
    public bool RR_OK;
    public bool DZ_Tetect_OK;
    public bool HunBai;
    public float KuanDu_Add;
    public short From_SSD_No;
    public short TuoPan_No;
    private ushort readCount = 636;
    private bool[] BoolRead = new bool[16];
    private int length;
    private short stringlength = 30;
    
    public ushort ReadCount
    {
        get { return readCount; }
        set { readCount = value; }
    }
    private IByteTransform ByteTransform = new ReverseBytesTransform();
    
           public void ParseSource(byte[] Content)
       {
           Id = ByteTransform.TransInt32(Content, 0);
           BianHao = ByteTransform.TransString(Content, 6, Content[5], Encoding.ASCII);
           HouDu = ByteTransform.TransInt16(Content, 36);
           ChangDu = ByteTransform.TransInt16(Content, 38);
           KuanDu = ByteTransform.TransInt16(Content, 40);
           ZhiJing = ByteTransform.TransInt16(Content, 42);
           Treatment = ByteTransform.TransString(Content, 48, ByteTransform.TransInt16(Content, 46) * 2, Encoding.BigEndianUnicode);
           ZhongLiang = ByteTransform.TransString(Content, 112, ByteTransform.TransInt16(Content, 110) * 2, Encoding.BigEndianUnicode);
           DengJi = ByteTransform.TransString(Content, 176, ByteTransform.TransInt16(Content, 174) * 2, Encoding.BigEndianUnicode);
           DJ_class = ByteTransform.TransInt16(Content, 236);
           JianYanYuan = ByteTransform.TransString(Content, 242, ByteTransform.TransInt16(Content, 240) * 2, Encoding.BigEndianUnicode);
           RiQi = ByteTransform.TransString(Content, 306, ByteTransform.TransInt16(Content, 304) * 2, Encoding.BigEndianUnicode);
           Code = ByteTransform.TransString(Content, 370, ByteTransform.TransInt16(Content, 368) * 2, Encoding.BigEndianUnicode);
           PiCi = ByteTransform.TransString(Content, 434, ByteTransform.TransInt16(Content, 432) * 2, Encoding.BigEndianUnicode);
           No_Pack = ByteTransform.TransBool(Content, 494);
           Print_Template = ByteTransform.TransString(Content, 450, ByteTransform.TransInt16(Content, 498) * 2, Encoding.BigEndianUnicode);
           KeHu_Name = ByteTransform.TransString(Content, 564, ByteTransform.TransInt16(Content, 562) * 2, Encoding.BigEndianUnicode);
           BoolRead = ByteTransform.TransBool(ByteTransform.TransByte(Content,624,2), 0, 16); // 明确位序);
           KH_OK = BoolRead[0];
           Print_Top_OK = BoolRead[1];
           Print_Body_OK = BoolRead[2];
           Need_QPM = BoolRead[3];
           Need_ZG = BoolRead[4];
           Need_PMD = BoolRead[5];
           IS_HouPMD = BoolRead[6];
           Double_PMD = BoolRead[7];
           Need_TieBiao_CM = BoolRead[8];
           Need_TieBiao_Middle = BoolRead[9];
           Multi_MiddleTieBiao = BoolRead[10];
           LM_OK = BoolRead[11];
           RR_OK = BoolRead[12];
           DZ_Tetect_OK = BoolRead[13];
           From_SSD_No = ByteTransform.TransInt16(Content, 626);
           TuoPan_No = ByteTransform.TransInt16(Content, 628);
           HunBai = ByteTransform.TransBool(Content, 630);
           KuanDu_Add = ByteTransform.TransSingle(Content, 632);
       }
        public byte[] ToSource()
        {
            byte[] buffer = new byte[ReadCount];
            ByteTransform.TransByte(Id).CopyTo(buffer, 0);
            buffer[4] = 30;
            buffer[5] = ByteTransform.TransByte((short)BianHao.Length)[1];
            ByteTransform.TransByte(BianHao, Encoding.ASCII).CopyTo(buffer, 6);
            ByteTransform.TransByte(HouDu).CopyTo(buffer, 36);
            ByteTransform.TransByte(ChangDu).CopyTo(buffer, 38);
            ByteTransform.TransByte(KuanDu).CopyTo(buffer, 40);
            ByteTransform.TransByte(ZhiJing).CopyTo(buffer, 42);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 44);
            length = Treatment.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 46);
            ByteTransform.TransByte(Treatment, Encoding.BigEndianUnicode).CopyTo(buffer, 48);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 108);
            length = ZhongLiang.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 110);
            ByteTransform.TransByte(ZhongLiang, Encoding.BigEndianUnicode).CopyTo(buffer, 112);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 172);
            length = DengJi.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 174);
            ByteTransform.TransByte(DengJi, Encoding.BigEndianUnicode).CopyTo(buffer, 176);
            ByteTransform.TransByte(DJ_class).CopyTo(buffer, 236);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 238);
            length = JianYanYuan.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 240);
            ByteTransform.TransByte(JianYanYuan, Encoding.BigEndianUnicode).CopyTo(buffer, 242);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 302);
            length = RiQi.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 304);
            ByteTransform.TransByte(RiQi, Encoding.BigEndianUnicode).CopyTo(buffer, 306);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 366);
            length = Code.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 368);
            ByteTransform.TransByte(Code, Encoding.BigEndianUnicode).CopyTo(buffer, 370);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 430);
            length = PiCi.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 432);
            ByteTransform.TransByte(PiCi, Encoding.BigEndianUnicode).CopyTo(buffer, 434);
            ByteTransform.TransByte(No_Pack).CopyTo(buffer, 494);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 496);
            length = Print_Template.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 498);
            ByteTransform.TransByte(Print_Template, Encoding.BigEndianUnicode).CopyTo(buffer, 500);
            ByteTransform.TransByte(stringlength).CopyTo(buffer, 560);
            length = KeHu_Name.Length;
            ByteTransform.TransByte((short)length).CopyTo(buffer, 562);
            ByteTransform.TransByte(KeHu_Name, Encoding.BigEndianUnicode).CopyTo(buffer, 564);
            int boolValues = 0;
            SetBitValue(ref boolValues, 8, KH_OK);
            SetBitValue(ref boolValues, 9, Print_Top_OK);
            SetBitValue(ref boolValues, 10, Print_Body_OK);
            SetBitValue(ref boolValues, 11, Need_QPM);
            SetBitValue(ref boolValues, 12, Need_ZG);
            SetBitValue(ref boolValues, 13, Need_PMD);
            SetBitValue(ref boolValues, 14, IS_HouPMD);
            SetBitValue(ref boolValues, 15, Double_PMD);
            SetBitValue(ref boolValues, 0, Need_TieBiao_CM);
            SetBitValue(ref boolValues, 1, Need_TieBiao_Middle);
            SetBitValue(ref boolValues, 2, Multi_MiddleTieBiao);
            SetBitValue(ref boolValues, 3, LM_OK);
            SetBitValue(ref boolValues, 4, RR_OK);
            SetBitValue(ref boolValues, 5, DZ_Tetect_OK);

            ByteTransform.TransByte((short)boolValues).CopyTo(buffer, 624);
            ByteTransform.TransByte(From_SSD_No).CopyTo(buffer, 626);
            ByteTransform.TransByte(TuoPan_No).CopyTo(buffer, 628);
            ByteTransform.TransByte(HunBai).CopyTo(buffer, 630);
            ByteTransform.TransByte(KuanDu_Add).CopyTo(buffer, 632);
            return buffer;
        }
       private void SetBitValue(ref int value,int index,bool bValue)
        {
            if (index<33)
            {
                if (bValue)
	            {
                    value += 1 << index;
	            }
                
            }
        }
}